#!/bin/bash

log_path="/data/logs/warn/"
mkdir -p $log_path
log_file="warn.log"

# 发送方法
function sendMsg() {
    #变量-企微企业id
    corpid="ww**************"
    #企微密钥
    secret="HM*******************************"

    headers="Content-Type: application/json"

    #1.获取token请求URL
    getTokenUrl="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpid}&corpsecret=${secret}&debug=1"
    # 发送请求
    tokenResponse=$(curl -s -X -POST -H "$headers" -d "" "$getTokenUrl")
    echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") 请求token返回内容：$tokenResponse" >> ${log_path}${log_file}
    token=$(echo "$tokenResponse" | jq -r '.access_token')
    echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") token：$token" >> ${log_path}${log_file}

    #2.发送应用消息
    sendUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${token}"
    noticeBody="{
      \"touser\": \"LiuJunWei|TanXiaoJi|FanJiaYao|ZhangXuLei\",
      \"msgtype\": \"text\",
      \"agentid\": 1000005,
      \"text\": {
        \"content\": \"$1\"
      }
    }"

    sendResponse=$(curl -s "$sendUrl" -H "$headers" -d "$noticeBody")

    echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") 发送通知返回结果：$sendResponse" >> ${log_path}${log_file}
}
